#include "qedacomponent.h"
#include "qedalibrary.h"
#include "componentwindow.h"

#include <QtCore/qdebug.h>
#include <QtGui/qmdiarea.h>
#include <QtGui/qmdisubwindow.h>
#include <QtXml/qdom.h>

QedaComponent::QedaComponent(QObject *parent) :
    QObject(parent)
{
    m_modified = false;
    m_win = 0;
    m_flags = SymbolPatternFlags;
}

QedaComponent::~QedaComponent()
{
    closeWin();
}

void QedaComponent::closeWin(bool quietMode)
{
    if (m_win)
    {
        QMdiSubWindow *subWin = m_win;
        ComponentWindow *w = qobject_cast<ComponentWindow*>(subWin->widget());
        if (quietMode)
            w->restore();
        QMdiArea *mdiArea = subWin->mdiArea();
        if (w->close())
        {
            mdiArea->setActiveSubWindow(subWin);
            mdiArea->closeActiveSubWindow();
        }
    }
}

QString QedaComponent::displayName() const
{
    QString dispName = m_name;
    if (m_modified)
        dispName += "*";
    return dispName;
}

void QedaComponent::fromXml(const QDomElement& elem)
{
    if (elem.hasAttribute("name"))
        m_name = elem.attribute("name");
    QDomNode n = elem.firstChild();
    while(!n.isNull())
    {
        QDomElement e = n.toElement();
        if(!e.isNull())
        {
            if (e.tagName() == "ref")
                m_reference = e.text();
            else if (e.tagName() == "group")
                m_group = e.text();
            else if (e.tagName() == "flags")
                m_flags = static_cast<QedaComponent::ComponentFlags>(e.text().toUInt(0, 16));
        }
        n = n.nextSibling();
    }
}

QedaLibrary *QedaComponent::library()
{
    return qobject_cast<QedaLibrary*>(parent());
}

void QedaComponent::setFlags(ComponentFlags flags, ComponentFlags mask)
{
    if ((m_flags & mask) != (flags & mask))
    {
        m_modified = true;
        m_flags &= ~(mask);
        m_flags |= (flags & mask);
    }
}

void QedaComponent::setLibrary(QedaLibrary *newLibrary)
{
    if (library() != newLibrary)
    {
        m_modified = true;
        library()->removeComponent(this);
        newLibrary->addComponent(this);
        setParent(newLibrary);
    }
}

void QedaComponent::setName(const QString &name)
{
    if (m_name != name)
    {
        m_modified = true;
        m_name = name;
    }
}

void QedaComponent::setReference(const QString &reference)
{
    if (m_reference != reference)
    {
        m_modified = true;
        library()->updateReferences(m_reference, reference);
        m_reference = reference;
    }
}

void QedaComponent::setGroup(const QString &group)
{
    if (m_group != group)
    {
        m_modified = true;
        library()->updateGroups(m_group, group);
        m_group = group;
    }
}

void QedaComponent::setModified(bool modified)
{
    if (m_modified != modified)
    {
        m_modified = modified;
        update();
    }
}

void QedaComponent::toXml(QDomDocument &doc, QDomElement &elem)
{
    elem.setAttribute("name", m_name);

    if (!m_reference.isEmpty())
    {
        QDomElement refNode = doc.createElement("ref");
        QDomText refText = doc.createTextNode(m_reference);
        refNode.appendChild(refText);
        elem.appendChild(refNode);
    }
    if (!m_group.isEmpty())
    {
        QDomElement groupNode = doc.createElement("group");
        QDomText groupText = doc.createTextNode(m_group);
        groupNode.appendChild(groupText);
        elem.appendChild(groupNode);
    }
    QDomElement flagsNode = doc.createElement("flags");
    QDomText flagsText = doc.createTextNode(QString::number(m_flags, 16));
    flagsNode.appendChild(flagsText);
    elem.appendChild(flagsNode);
}

void QedaComponent::update()
{
    if (m_modified && library())
        library()->setModified(true);
    emit displayNameUpdated(displayName());
    emit componentUpdated(this);
}

